package com.my_test.mybatis_plus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyalin@act-telecom.com
 * @date 2019-05-20 17:05
 **/
public class MybatisPlusGenteratorCode {

//    private final static String DRIVER = "com.mysql.jdbc.Driver";
//private final static String DATA_URL = "jdbc:mysql://localhost:3306/webmagic";
//private final static String USERNAME = "root";
//    private final static String PASSWORD = "123456";
    private final static String DRIVER = "oracle.jdbc.driver.OracleDriver";
    private final static String DATA_URL = "jdbc:oracle:thin:@172.31.134.222:1521:orcl";
    private final static String USERNAME = "app_ms";
    private final static String PASSWORD = "app_ms";
    private final static String ROOT_DIR = "D:/MybatisGenteratorCode";
    private final static String AUTHOR = "";

    private final static String TABLE_PREFIX = "";
    private final static String[] TABLES = {"APP_HANDLE"};

    public static void main(String[] args) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.ORACLE)
                .setDriverName(DRIVER)
                .setUrl(DATA_URL)
                .setUsername(USERNAME)
                .setPassword(PASSWORD);

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(ROOT_DIR)
                .setFileOverride(true)
                .setActiveRecord(true)
                .setIdType(IdType.AUTO)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setAuthor(AUTHOR)
                .setEntityName("%s")
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setTablePrefix(TABLE_PREFIX)
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityTableFieldAnnotationEnable(true)
                .setInclude(TABLES);

        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("java")
                .setController("controller")
                .setService("service")
                .setServiceImpl("service.impl")
                .setEntity("entity")
                .setMapper("mapper");

        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
                this.setMap(map);
            }
        };
        //xml生成路径
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return ROOT_DIR + "/resources/" + tableInfo.getEntityName().replace("Entity", "") + "Mapper.xml";
            }
        });
        injectionConfig.setFileOutConfigList(focList);

        // 关闭默认 xml 生成，调整生成 至 根目录
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);

        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(dataSourceConfig)
                .setGlobalConfig(globalConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setCfg(injectionConfig)
                .setTemplate(templateConfig);

        autoGenerator.execute();
    }
}
